#!/usr/bin/perl
use XML::Simple;

my $xmlFile = $ARGV[1];
$xmlFile = "./xml/web.xml" unless(defined $xmlFile); 
my $xml = XMLin($xmlFile);
printHashTree($xml);

1;

sub printHashTree(){
    my ($h, $depth) = @_;
    $depth = 0 unless defined $depth;
    my $space = ' ' x ($depth * 2);

    if ( not ref($h) eq 'HASH'){
        print $space . "$h\n";
        return 0;
    }
    
    while (my($k, $v) = each %$h) {
        if ( ref($v) eq 'HASH' ){
            print $space . "<$k>\n";
            printHashTree($v, $depth + 1);
            print $space . "</$k>\n";
        } elsif (ref($v) eq 'ARRAY'){
            print $space . "<$k>\n";
            foreach (@$v){
                print $space . "  [\n";
                printHashTree($_, $depth + 1);
                print $space . "  ]\n";
            }
            print $space . "</$k>\n";
            
        } else {
            print $space . "$k = $v \n";
        }
    }

    return 0;
}

